package com.lagou.rpc.client.netty.loadbalance;

import com.lagou.rpc.common.entity.HostPortMetaData;
import com.lagou.rpc.client.netty.Loadbalancer;

import java.util.List;
import java.util.Optional;

/**
 * 快速响应负载均衡策略
 *
 * @author wlz
 * @date 2020/5/12
 */
public class FastResponseLoadbalancer implements Loadbalancer {

    @Override
    public HostPortMetaData balance(List<HostPortMetaData> hostPortMetaDataList) {
        hostPortMetaDataList.sort((h1, h2) ->
                (int) (Optional.ofNullable(h1.getLastResponseCostTime()).orElse(0L) - Optional.ofNullable(h2.getLastResponseCostTime()).orElse(0L))
        );
        return hostPortMetaDataList.get(0);
    }
}
